﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


using Daidalos;
using Microsoft.DirectX.DirectSound;
using Daidalos.Exception;

namespace Daidalos.Sound
{
    /// <summary>
    /// 
    /// </summary>
    public class Sound
    {
        /// <summary>
        /// 
        /// </summary>
        private Microsoft.DirectX.DirectSound.Buffer buffer = null;

        /// <summary>
        /// 
        /// </summary>
        private Microsoft.DirectX.DirectSound.Buffer3D buffer3d = null;

        /// <summary>
        /// 
        /// </summary>
        private double frequency = 1.0d;

        /// <summary>
        /// 
        /// </summary>
        private double volume = 1.0d;

        /// <summary>
        /// 
        /// </summary>
        private bool loop = false;

        /// <summary>
        /// 
        /// </summary>
        private bool has3D = false;

        /// <summary>
        /// 
        /// </summary>
        public double Frequency
        {
            get
            {
                return frequency;
            }
            set
            {
                frequency = value;
                UpdateSetup();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public double Volume
        {
            get
            {
                return volume;
            }
            set
            {
                volume = value;
                UpdateSetup();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Loop
        {
            get
            {
                return loop;
            }
            set
            {
                loop = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Load(string name)
        {
            buffer = new Buffer(name, SoundManager.Instance.Device);
            Volume = 0;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool Load(string name, bool is3D)
        {
            if (!is3D)
                return Load(name);

            BufferDescription desc = new BufferDescription();
            desc.Control3D = true;

            buffer = new Buffer(name, desc, SoundManager.Instance.Device);

            buffer3d = new Buffer3D(buffer);
            buffer3d.Mode = Mode3D.Normal;

            has3D = true;

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        public void PlayFromPosition(float x, float y, float z)
        {
            if (!has3D)
                throw new GenericException("Sound is not 3-Dimensional");
            buffer3d.Position = new Microsoft.DirectX.Vector3(x, y, z);
            Play();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        public void SetPosition(float x, float y, float z)
        {
            if (!has3D)
                throw new GenericException("Sound is not 3-Dimensional");
            buffer3d.Position = new Microsoft.DirectX.Vector3(x, y, z);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Play()
        {
            if(Loop)
                buffer.Play(0, BufferPlayFlags.Looping);
            else
                buffer.Play(0, BufferPlayFlags.Default);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Pause()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateSetup()
        {
            //buffer.Frequency = (int)(Frequency * 200) * buffer.Format.SamplesPerSecond / 100;
            int vol = -5000 + (int)System.Math.Floor(Volume * 10000.0d);
            if (vol > 0)
                vol = 0;
            buffer.Volume = vol;
        }
    }
}
